<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

///////////////////////////////////////////////////////////////////////////////
function ws_comment_load($target, $type, $page = 1) {
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/comment/comment.class.php');
	$mObj = new CommentBase();
	$mObj->setTarget($target);
	
	if( $type == 'content_comment' ) {
		$type = COMMENT_TYPE_CONTENT;
	} elseif( $type == 'news_comment' ) {
		$type = COMMENT_TYPE_NEWS;
	} elseif( $type == 'page_comment' ) {
		$type = COMMENT_TYPE_PAGE;
	} elseif( $type == 'product_comment' ) {
		$type = COMMENT_TYPE_PRODUCT;
	}
	$mObj->setType($type);
	
	//
	$limit_total = $mObj->getItemsTotal();
	$mObj->setcachetime('hour');
	$items = $mObj->cache('getItems', array('', 0, true));
	if( is_array($items) ) {
		$items = util_stripslashes($items);
	}
	
	//
	$r = array();
	$r['total'] = $limit_total;
	$r['items'] = $items;
	
	$objResponse = new xajaxResponse();
	$objResponse->addScriptCall("comment_load_cb", $r);
	
	return $objResponse;
}

function ws_comment_add($data_array, $f = '') {
	$data_array = util_htmlfilter($data_array);
	$data_array['field_author'] = util_substr($data_array['field_content'], 0, 50);
	$data_array['field_content'] = util_substr($data_array['field_content'], 0, 4000);
	$target = $data_array['target'];
	$type = $data_array['form'];
	
	//
	if( isset($data_array['checkcode']) && isset($_SESSION['checkcode']) ) {
		if( $data_array['checkcode'] !== $_SESSION['checkcode'] ) {
			return 	_alert_message(_lang('ws:error_check_code'));
		}
	}
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/comment/comment.class.php');
	$mObj = new CommentBase();
	$mObj->setTarget($target);
	
	if( $type == 'content_comment' ) {
		$type = COMMENT_TYPE_CONTENT;
	} elseif( $type == 'news_comment' ) {
		$type = COMMENT_TYPE_NEWS;
	} elseif( $type == 'page_comment' ) {
		$type = COMMENT_TYPE_PAGE;
	} elseif( $type == 'product_comment' ) {
		$type = COMMENT_TYPE_PRODUCT;
	}
	$mObj->setType($type);
	
	//
	$mObj->setcachetime('hour');
	$mObj->cacheclean('getItems', array('', 0, true));
	$items_id = $mObj->addItem($data_array);
	
	//
	$r = array();
	$r['f'] = $f;
	if( $items_id > 0 ) {
		$r['status'] = 1;	
	} else {
		$r['status'] = 0;	
	}
	
	$objResponse = new xajaxResponse();
	$objResponse->addScriptCall("comment_add_cb", $r);
	
	return $objResponse;
}

///////////////////////////////////////////////////////////////////////////////
// Instantiate the xajax object.  No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();

//$xajax->debugOn(); // Uncomment this line to turn debugging on

// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
//
//
$xajax->registerFunction("ws_comment_load");
$xajax->registerFunction("ws_comment_add");

// Process any requests.  Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();
//
?>